package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApUnlikeBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.UnLikesBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApReadBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import com.mongodb.client.result.DeleteResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


@Service
@Slf4j
public class ApUnlikeBehaviorServiceImpl implements ApUnlikeBehaviorService {
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    MongoTemplate mongoTemplate;
    @Override
    public ResponseResult unlikeBehavior(UnLikesBehaviorDTO dto) {
        if (StringUtils.isEmpty(dto.getArticleId())){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"文章id不能为空");
        }
        ApUser user = AppThreadLocalUtils.getUser();
        if (StringUtils.isEmpty(user.getId())){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"未登录，请先登录");
        }
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), null);
        Query query = Query.query(Criteria.where("articleId").is(dto.getArticleId()).and("entryId").is(apBehaviorEntry.getId()));
        ApUnlikesBehavior apUnlikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
            log.info("查询不喜欢行为是否存在" + apUnlikesBehavior);
        if (dto.getType()==0){

            if (apUnlikesBehavior == null) {//不存在
                ApUnlikesBehavior apLikesBehavior2 = ApUnlikesBehavior.builder()
                        .entryId(apBehaviorEntry.getId())
                        .articleId(dto.getArticleId())
                        .build();
                ApUnlikesBehavior saveResult = mongoTemplate.insert(apLikesBehavior2);
                return ResponseResult.okResult(saveResult);
            }else{
                CustException.cust(AppHttpCodeEnum.USER_AGAIN_LIKE,"请勿重复操作");
            }
        }else{
            if (apUnlikesBehavior==null){
                CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"操作无效");
            }
            DeleteResult removeResult = mongoTemplate.remove(query, ApUnlikesBehavior.class);
            log.info("用户取消不喜欢成功"+removeResult);
            return ResponseResult.okResult(removeResult);
        }
        return null;
    }
}
